package com.apchernykh.rozhlasapp.ui.podcasts;

import java.util.ArrayList;
import java.util.Arrays;

import com.apchernykh.ceskyrozhlas.basic.R;
import com.apchernykh.rozhlasapp.ui.BaseDetailsFragment;
import com.apchernykh.rozhlasapp.ui.RozhlasAppBaseFragmentActivity;
import com.apchernykh.types.PodcastFeedInfo;

import android.content.Intent;
import android.os.Bundle;
import android.support.v4.app.FragmentTransaction;


import android.view.View;
import android.widget.ListView;

public class FeedsListFragment extends android.support.v4.app.ListFragment {
	FeedListAdapter mFeedsListAdapter;
	private int mCurCheckPosition;
	private boolean mDualPane;

	@Override
	public void onActivityCreated(Bundle iSavedInstanceState) {
		super.onActivityCreated(iSavedInstanceState);

		ArrayList<String> aFeedNames = new ArrayList<String>(
				Arrays.asList(getResources().getStringArray(
						R.array.podcasts_titles)));
		ArrayList<String> aFeedURLs = new ArrayList<String>(
				Arrays.asList(getResources().getStringArray(
						R.array.podcasts_urls)));

		if (RozhlasAppBaseFragmentActivity.mApplicationModel != null) {
			int aFeedsTargetSize = Math.min(aFeedNames.size(), aFeedURLs.size());
			RozhlasAppBaseFragmentActivity.mApplicationModel.mFeeds = new ArrayList<PodcastFeedInfo>(
					aFeedsTargetSize );
			for (int aFeedItemNumber = 0; aFeedItemNumber < aFeedsTargetSize; aFeedItemNumber++) {
				RozhlasAppBaseFragmentActivity.mApplicationModel.mFeeds
						.add(new PodcastFeedInfo(aFeedNames.get(aFeedItemNumber), aFeedURLs
								.get(aFeedItemNumber)));
			}

			mFeedsListAdapter = new FeedListAdapter(getActivity(),
					R.layout.feed_info,
					RozhlasAppBaseFragmentActivity.mApplicationModel.mFeeds);
			setListAdapter(mFeedsListAdapter);
		}

		else {
			setEmptyText("Could not find feeds list");
		}
		// fragment directly in the containing UI.
		View detailsFrame = getActivity().findViewById(R.id.feed_details);
		mDualPane = detailsFrame != null
				&& detailsFrame.getVisibility() == View.VISIBLE;

		if (iSavedInstanceState != null) {
			// Restore last state for checked position.
			mCurCheckPosition = iSavedInstanceState.getInt("curChoice", 0);
		}

		if (mDualPane) {
			// In dual-pane mode, the list view highlights the selected item.
			getListView().setChoiceMode(ListView.CHOICE_MODE_SINGLE);
			// Make sure our UI is in the correct state.
			showDetails(mCurCheckPosition);
		}
	}

	private void showDetails(int index) {
		mCurCheckPosition = index;
		if (mDualPane) {
			BaseDetailsFragment aDetailsFragment = (BaseDetailsFragment) getFragmentManager()
					.findFragmentById(R.id.feed_details);
			if (aDetailsFragment == null || aDetailsFragment.getShownIndex() != index) {
				// Make new fragment to show this selection.
				aDetailsFragment = FeedDetailsFragment.newInstance(index);

				// Execute a transaction, replacing any existing fragment
				// with this one inside the frame.
				FragmentTransaction aFragmentTransaction = getFragmentManager()
						.beginTransaction();
				aFragmentTransaction.replace(R.id.feed_details, aDetailsFragment);
				aFragmentTransaction.setTransition(FragmentTransaction.TRANSIT_FRAGMENT_FADE)
						.commit();
			}
		} else {
			Intent aNewActivityIntent = new Intent();
			aNewActivityIntent.setClass(getActivity(), FeedDetailsActivity.class);
			aNewActivityIntent.putExtra("index", index);
			startActivity(aNewActivityIntent);
		}
	}

	@Override
	public void onListItemClick(ListView iListView, View iView, int iPosition, long iViewId) {
		showDetails(iPosition);
		getListView().setItemChecked(iPosition, true);
	}

	@Override
	public void onSaveInstanceState(Bundle oStateToSave) {
		// TODO Auto-generated method stub
		super.onSaveInstanceState(oStateToSave);
		oStateToSave.putInt("curChoice", mCurCheckPosition);
	}

}
